﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.Configuration;
using System.Security;
using BlackBox.Models;

namespace BlackBox.Controllers
{
    public class HomeController : Controller
    {
        #region Home ViewModel
        public class EntryViewModel
        {
            public IEnumerable<News> News { get; set; }
            public IEnumerable<Game> Game { get; set; }
            public UserProfiles UserData { get; set; }
            public Game GameData { get; set; }
            public News NewsData { get; set; }
			public Group GroupData { get; set; }
            public IEnumerable<ActiveGame> ActiveGame { get; set; }
        }
        #endregion

        #region Repositories
        NewsRepository repNews = new NewsRepository();
        GameRepository repGame = new GameRepository();
        UserRepository repUser = new UserRepository();
		GroupRepository repGroup = new GroupRepository();
		#endregion

		#region Main
		public ActionResult Main()
        {
            EntryViewModel model = new EntryViewModel();

            model.News = repNews.GetLatestnews();
            model.Game = repGame.GetAllGames();
            model.ActiveGame = repGame.GetAllActiveGames();
			
            return View(model);
        }
		#endregion

		#region ChatRoom
		public ActionResult ChatRoom()
        {
            return View();
        }
		#endregion

		#region FAQ
		public ActionResult FAQ()
        {
            return View();
        }
		#endregion

		#region SendMail
		[HttpPost]
        public ActionResult SendMail(FormCollection formData)
        {
            var question = "No Question";
            var email = "No email";
            foreach (var key in formData.AllKeys)
            {
                foreach (var val in formData.GetValues(key))
                {
                    //Geting the val from the form with the key  "Question"
                    if (key == "Question")
                    {
                        question = val;
                    }

                    if (key == "Email")
                    {
                        email = val;
                    }
                }
            }

            //Creating a Email
            MailMessage newEmail = new MailMessage();

            newEmail.To.Add(ConfigurationManager.AppSettings["Email"]);
            newEmail.Subject = "A Question from a user";
            newEmail.Body = question + " this question is from " + email;

            SmtpClient client = new SmtpClient();

            client.EnableSsl = true;
            client.Send(newEmail);

            //Redirect the user to the main Page
            return RedirectToAction("Main", "Home");
        }
		#endregion

        #region WarningMail
        /*
        [HttpPost]
        public ActionResult WarningMail(FormCollection formData)
        {
            var message = "Error occurred.";
            var email = "Error occurred.";
            foreach (var key in formData.AllKeys)
            {
                foreach (var val in formData.GetValues(key))
                {
                    if (key == "Message")
                    {
                        message = val;
                    }

                    if (key == "Email")
                    {
                        email = val;
                    }
                }
            }

            MailMessage newEmail = new MailMessage();

            newEmail.To.Add();
            newEmail.Subject = "Warning from BoardGames.is";
            newEmail.Body = "Hello there, this is a warning from the administrators of BoardGames.is, " +
            "if your poor conduct persists we will be forced to ban your account without a chance of redemption.";

            SmtpClient client = new SmtpClient();

            client.EnableSsl = true;
            client.Send(newEmail);

            return RedirectToAction("Main", "Home");
        }
        */
        #endregion

		#region News
		public ActionResult News()
        {
            var model = repNews.GetAllNews();

            return View(model);
        }
		#endregion

		#region EditNews (Get)
		[HttpGet]
        [Authorize(Roles = "Admin")]
        public ActionResult EditNews(int id)
        {
            var model = repNews.GetNewsByID(id);

            return View(model);
        }
		#endregion

		#region EditNews (Post)
		[HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult EditNews(News newNews)
        {
			repNews.UpdateNews(newNews);
			return RedirectToAction("News", "Home");
        }
		#endregion

        #region Add News (Post)
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult AddNews(EntryViewModel data)
        {
            var newNews = data.NewsData;
            repNews.AddNews(newNews);
            return RedirectToAction("News", "Home");
        }
        #endregion

        #region Searchbox
        public ActionResult SearchBox(string s)
        {
            EntryViewModel model = new EntryViewModel();

            model.GameData = repGame.GetGameByName(s);
            model.UserData = repUser.GetByName(s);
            model.NewsData = repNews.GetByName(s);
			model.GroupData = repGroup.GetByName(s);

            return View(model);
		}
		#endregion
	}
}
